﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MapCube : MonoBehaviour
{
    [HideInInspector]
    public GameObject builded_turret;
    [HideInInspector]
    public TurretData builded_type;
    private bool is_upgrade = false;
    private int id;
    public Renderer cube_renderer;

    private void Awake()
    {
        id = transform.GetSiblingIndex();
        cube_renderer = GetComponent<Renderer>();
    }

    public void UpgradeTurret()
    {
        Destroy(builded_turret);
        GameObject go = GameObject.Instantiate(builded_type.up_turret_type, transform.position, Quaternion.identity);
        builded_turret = go;
        is_upgrade = true;
    }

    public void DestroyTurret()
    {
        is_upgrade = false;
        Destroy(builded_turret);
    }

    public bool BuildTurret(TurretData selected_turret)
    {
        if (builded_turret == null)
        {
            GameObject go = GameObject.Instantiate(selected_turret.turret_type, transform.position, Quaternion.identity);
            BuildManager.selected_pre.SetActive(false);
            cube_renderer.material.color = Color.red;
            builded_type = selected_turret;
            builded_turret = go;
            return true;
        }
        return false;
    }

    public void GetUpgradeUI(GameObject upgradeUI,Animator UI_anim,Button upgrade_button)  //显示升级面板
    {
        if (builded_turret != null) {
            if (is_upgrade == false)
            {
                upgrade_button.interactable = true;
            }
            else
            {
                upgrade_button.interactable = false;
            }
            if (BuildManager.cube_id == id)  //显示UI的逻辑
            {
                if(upgradeUI.activeInHierarchy == true)
                {
                    UI_anim.SetTrigger("hideUI");
                }
                else
                {
                    upgradeUI.SetActive(true);
                }
            }
            else
            {
                upgradeUI.transform.position = transform.position;
                upgradeUI.SetActive(false); //这里刷新一下upgradeUI的动画
                upgradeUI.SetActive(true);
            }
            BuildManager.cube_id = id;
        }
    }

    private void OnMouseEnter()
    {
        BuildManager.skim_cube = this;
        if(BuildManager.selected_pre !=null )
        {
            if (builded_turret == null)
            {
                BuildManager.selected_pre.transform.position = transform.position;
                BuildManager.selected_pre.SetActive(true); //放置预设
                cube_renderer.material.color = Color.green; //改变颜色
            }
            else
            {
                cube_renderer.material.color = Color.red;
            }
        }
    }

    private void OnMouseExit()
    {
        if (builded_turret == null && BuildManager.selected_pre != null)
        {
            BuildManager.selected_pre.SetActive(false);
        }
        cube_renderer.material.color = Color.white;
    }
}
